package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2022/11/8 9:41
 * @Version 1.0
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {


    @Resource
    private ApArticleMapper articleMapper;
    @Resource
    private ApArticleConfigMapper configMapper;
    @Resource
    private ApArticleContentMapper contentMapper;
    @Autowired
    private ArticleDetailService articleDetailService;

    private final static Integer DEFAULT_SIZE = 10;

    @Override
    public ResponseResult loadArticle(ArticleHomeDto homeDto, Integer type) {
        paramCheck(homeDto);
        return ResponseResult.okResult(articleMapper.loadArticle(homeDto,type));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveArticle(ArticleDto dto) throws Exception{
        if(StringUtils.isEmpty(dto.getContent()) || StringUtils.isEmpty(dto.getTitle())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        apArticle.setPublishTime(new Date());

        if(dto.getId() == null){
            save(apArticle);
            ApArticleConfig config = new ApArticleConfig();
            config.setArticleId(apArticle.getId());
            config.setIsForward(true);
            config.setIsDown(false);
            config.setIsDelete(false);
            config.setIsComment(true);
            configMapper.insert(config);

            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            contentMapper.insert(content);
        }else {
            updateById(apArticle);
            ApArticleContent content = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            content.setContent(dto.getContent());
            contentMapper.updateById(content);
        }

        //生成静态资源文件
        articleDetailService.createStaticHtml(dto.getContent(),apArticle.getId());
        return ResponseResult.okResult(apArticle.getId());
    }

    private void paramCheck(ArticleHomeDto homeDto){
        //基本判断
        if(homeDto.getMinBehotTime() == null) {
            homeDto.setMinBehotTime(new Date());
        }
        if(homeDto.getMaxBehotTime() == null) {
            homeDto.setMaxBehotTime(new Date());
        }
        if(homeDto.getSize() == null){
            homeDto.setSize(DEFAULT_SIZE);
        }
        if(StringUtils.isEmpty(homeDto.getTag())){
            homeDto.setTag("__all__");
        }
    }
}